#include "WiFly.h"
#include "Credentials.h"

Server server(1919);
int photo = 0;
int lumi;

void setup() {
  WiFly.begin();
  WiFly.enableDHCP();
  if (!WiFly.join(ssid, passphrase,true)) {
    while (1) {
      // Hang on failure.
    }
  }

  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
  
  server.begin();
  delay(1000);
}

void loop() {
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    //boolean current_line_is_blank = true;
    if (client.connected()) {
      while(client.available() < 1);
      char c1 = client.read();
      while(client.available() < 1);
      char c2 = client.read();

      if(c1=='#' && c2=='4'){
        getLumi(photo);
        client.print("LUM ");
        client.println(lumi);
      }
      if(c1=='#' && c2=='?'){
        client.println("OK LUM");
      }
      
      client.stop();
    }
    delay(100);
  }
}

void getLumi(int pin){
  lumi = analogRead(pin);
  lumi = map(lumi, 0, 900, 100, -15);
}
